{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This example is kindly contributed by [FreddyBaudine](https://github.com/FreddyBaudine) for reproducing [pygae/galgebra#26](https://github.com/pygae/galgebra/issues/26) and [pygae/galgebra#30](https://github.com/pygae/galgebra/issues/30) with modifications by [utensil](https://github.com/utensil).\n",
    "\n",
    "Please note before Python code, there's an invisible markdown cell with the following code to enable `color` and define some colors from http://latexcolor.com/:\n",
    "\n",
    "```markdown\n",
    "$$\n",
    "\\require{color}\n",
    "\\definecolor{airforceblue}{rgb}{0.36, 0.54, 0.66}\n",
    "\\definecolor{applegreen}{rgb}{0.55, 0.71, 0.0}\n",
    "\\definecolor{atomictangerine}{rgb}{1.0, 0.6, 0.4}\n",
    "$$\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "\\require{color}\n",
    "\\definecolor{airforceblue}{rgb}{0.36, 0.54, 0.66}\n",
    "\\definecolor{applegreen}{rgb}{0.55, 0.71, 0.0}\n",
    "\\definecolor{atomictangerine}{rgb}{1.0, 0.6, 0.4}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import sys\n",
    "from galgebra.printer import  Format, xpdf, latex\n",
    "Format()\n",
    "from sympy import symbols, sin, pi, Array, permutedims\n",
    "from galgebra.ga import Ga"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import Math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Base manifold (three dimensional)\n",
    "## Metric tensor (cartesian coordinates - norm = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import cos, sin, symbols\n",
    "g3coords = (x,y,z) = symbols('x y z')\n",
    "g3 = Ga('ex ey ez', g = [1,1,1], coords = g3coords,norm=False) # Create g3\n",
    "(e_x,e_y,e_z) = g3.mv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle g =\\left[\\begin{array}{ccc}1 & 0 & 0\\\\0 & 1 & 0\\\\0 & 0 & 1\\end{array}\\right]$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Math(r'g =%s' % latex(g3.g))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Two dimensioanal submanifold - Unit sphere\n",
    "\n",
    "Basis not normalised"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "sp2coords = (theta, phi) = symbols(r'{\\color{airforceblue}\\theta} {\\color{applegreen}\\phi}', real = True)\n",
    "sp2param = [sin(theta)*cos(phi), sin(theta)*sin(phi), cos(theta)]\n",
    "\n",
    "sp2 = g3.sm(sp2param, sp2coords, norm = False) # submanifold\n",
    "\n",
    "(etheta, ephi) = sp2.mv() # sp2 basis vectors\n",
    "(rtheta, rphi) = sp2.mvr() # sp2 reciprocal basis vectors\n",
    "\n",
    "sp2grad = sp2.grad\n",
    "\n",
    "sph_map = [1, theta, phi]  # Coordinate map for sphere of r = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle (\\theta,\\phi)\\rightarrow (r,\\theta,\\phi) = \\left[ 1, \\  {\\color{airforceblue}\\theta}, \\  {\\color{applegreen}\\phi}\\right]$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Math(r'(\\theta,\\phi)\\rightarrow (r,\\theta,\\phi) = %s' % latex(sph_map))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle e_\\theta \\cdot e_\\theta = 1$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Math(r'e_\\theta \\cdot e_\\theta = %s' % latex(etheta|etheta))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle e_\\phi \\cdot e_\\phi = {\\sin{\\left ({\\color{airforceblue}\\theta} \\right )}}^{2}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Math(r'e_\\phi \\cdot e_\\phi = %s' % latex(ephi|ephi))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle g = \\left[\\begin{array}{cc}1 & 0\\\\0 & {\\sin{\\left ({\\color{airforceblue}\\theta} \\right )}}^{2}\\end{array}\\right]$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Math('g = %s' % latex(sp2.g))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle g^{-1} = \\left[\\begin{array}{cc}1 & 0\\\\0 & \\frac{1}{{\\sin{\\left ({\\color{airforceblue}\\theta} \\right )}}^{2}}\\end{array}\\right]$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Math(r'g^{-1} = %s' % latex(sp2.g_inv))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Christoffel symbols of the first kind: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "Cf1 = sp2.Christoffel_symbols(mode=1)\n",
    "Cf1 = permutedims(Array(Cf1), (2, 0, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\Gamma_{1, \\alpha, \\beta} = \\left[\\begin{matrix}0 & 0\\\\0 & - \\frac{\\sin{\\left (2 {\\color{airforceblue}\\theta} \\right )}}{2}\\end{matrix}\\right] \\quad \\Gamma_{2, \\alpha, \\beta} = \\left[\\begin{matrix}0 & \\frac{\\sin{\\left (2 {\\color{airforceblue}\\theta} \\right )}}{2}\\\\\\frac{\\sin{\\left (2 {\\color{airforceblue}\\theta} \\right )}}{2} & 0\\end{matrix}\\right] $"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# mat_str=None is needed due https://github.com/sympy/sympy/issues/19381\n",
    "Math(r'\\Gamma_{1, \\alpha, \\beta} = %s \\quad \\Gamma_{2, \\alpha, \\beta} = %s ' % (latex(Cf1[0, :, :], mat_str=None), latex(Cf1[1, :, :], mat_str=None)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "Cf2 = sp2.Christoffel_symbols(mode=2)\n",
    "Cf2 = permutedims(Array(Cf2), (2, 0, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\Gamma^{1}_{\\phantom{1,}\\alpha, \\beta} = \\left[\\begin{matrix}0 & 0\\\\0 & - \\frac{\\sin{\\left (2 {\\color{airforceblue}\\theta} \\right )}}{2}\\end{matrix}\\right] \\quad \\Gamma^{2}_{\\phantom{2,}\\alpha, \\beta} = \\left[\\begin{matrix}0 & \\frac{1}{\\tan{\\left ({\\color{airforceblue}\\theta} \\right )}}\\\\\\frac{1}{\\tan{\\left ({\\color{airforceblue}\\theta} \\right )}} & 0\\end{matrix}\\right] $"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Math(r'\\Gamma^{1}_{\\phantom{1,}\\alpha, \\beta} = %s \\quad \\Gamma^{2}_{\\phantom{2,}\\alpha, \\beta} = %s ' % (latex(Cf2[0, :, :], mat_str=None), latex(Cf2[1, :, :], mat_str=None)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "F = sp2.mv('F','vector',f=True) #scalar function\n",
    "f = sp2.mv('f','scalar',f=True) #vector function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\nabla = \\boldsymbol{e}_{{\\color{airforceblue}\\theta}} \\frac{\\partial}{\\partial {\\color{airforceblue}\\theta}} + \\boldsymbol{e}_{{\\color{applegreen}\\phi}} \\frac{1}{{\\sin{\\left ({\\color{airforceblue}\\theta} \\right )}}^{2}} \\frac{\\partial}{\\partial {\\color{applegreen}\\phi}}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Math(r'\\nabla = %s' % latex(sp2grad))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\nabla f = \\partial_{{\\color{airforceblue}\\theta}} f  \\boldsymbol{e}_{{\\color{airforceblue}\\theta}} + \\frac{\\partial_{{\\color{applegreen}\\phi}} f }{{\\sin{\\left ({\\color{airforceblue}\\theta} \\right )}}^{2}} \\boldsymbol{e}_{{\\color{applegreen}\\phi}}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Math(r'\\nabla f = %s' % latex(sp2.grad * f))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle F = F^{{\\color{airforceblue}\\theta}}  \\boldsymbol{e}_{{\\color{airforceblue}\\theta}} + F^{{\\color{applegreen}\\phi}}  \\boldsymbol{e}_{{\\color{applegreen}\\phi}}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Math(r'F = %s' % latex(F))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\nabla F = \\left ( \\frac{F^{{\\color{airforceblue}\\theta}} }{\\tan{\\left ({\\color{airforceblue}\\theta} \\right )}} + \\partial_{{\\color{airforceblue}\\theta}} F^{{\\color{airforceblue}\\theta}}  + \\partial_{{\\color{applegreen}\\phi}} F^{{\\color{applegreen}\\phi}} \\right )  + \\left ( \\frac{2 F^{{\\color{applegreen}\\phi}} }{\\tan{\\left ({\\color{airforceblue}\\theta} \\right )}} + \\partial_{{\\color{airforceblue}\\theta}} F^{{\\color{applegreen}\\phi}}  - \\frac{\\partial_{{\\color{applegreen}\\phi}} F^{{\\color{airforceblue}\\theta}} }{{\\sin{\\left ({\\color{airforceblue}\\theta} \\right )}}^{2}}\\right ) \\boldsymbol{e}_{{\\color{airforceblue}\\theta}}\\wedge \\boldsymbol{e}_{{\\color{applegreen}\\phi}}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Math(r'\\nabla F = %s' % latex((sp2.grad * F)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# One dimensioanal submanifold\n",
    "\n",
    "Basis not normalised"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "cir_th = phi = symbols(r'{\\color{atomictangerine}\\phi}',real = True)\n",
    "cir_map = [pi/8, phi]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle (\\phi)\\rightarrow (\\theta,\\phi) = \\left[ \\frac{\\pi}{8}, \\  {\\color{atomictangerine}\\phi}\\right]$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Math(r'(\\phi)\\rightarrow (\\theta,\\phi) = %s' % latex(cir_map))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "cir1d = sp2.sm( cir_map , (cir_th,), norm = False) # submanifold\n",
    "\n",
    "cir1dgrad = cir1d.grad\n",
    "\n",
    "(ephi) = cir1d.mv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle e_\\phi \\cdot e_\\phi = \\frac{1}{2} - \\frac{\\sqrt{2}}{4}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Math(r'e_\\phi \\cdot e_\\phi = %s' % latex(ephi[0] | ephi[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle g = \\left[\\begin{array}{c}\\frac{1}{2} - \\frac{\\sqrt{2}}{4}\\end{array}\\right]$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Math('g = %s' % latex(cir1d.g))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "h = cir1d.mv('h','scalar',f= True)\n",
    "\n",
    "H = cir1d.mv('H','vector',f= True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\nabla = \\boldsymbol{e}_{{\\color{atomictangerine}\\phi}} \\left ( 2 \\sqrt{2} + 4\\right ) \\frac{\\partial}{\\partial {\\color{atomictangerine}\\phi}}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Math(r'\\nabla = %s' % latex(cir1dgrad))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\nabla h = \\left(2 \\sqrt{2} + 4\\right) \\partial_{{\\color{atomictangerine}\\phi}} h  \\boldsymbol{e}_{{\\color{atomictangerine}\\phi}}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Math(r'\\nabla h = %s' % latex((cir1d.grad * h).simplify()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle H = H^{{\\color{atomictangerine}\\phi}}  \\boldsymbol{e}_{{\\color{atomictangerine}\\phi}}$"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Math('H = %s' % latex(H))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\nabla H = \\partial_{{\\color{atomictangerine}\\phi}} H^{{\\color{atomictangerine}\\phi}} $"
      ],
      "text/plain": [
       "<IPython.core.display.Math object>"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Math(r'\\nabla H = %s' % latex((cir1d.grad * H).simplify()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
